import tkinter as tk
from add_role_page import open_add_role_page
from delete_role_page import open_delete_role_page
import pymssql
#上级管理部门-查询角色信息的页面
def open_role_info(entry1_value):
    role_info_window = tk.Tk()
    role_info_window.title("上级主管部门-角色信息页面")
    role_info_window.geometry("350x350")

    # 提示文本
    label_prompt = tk.Label(role_info_window, text="请输入角色类型：")
    label_prompt.pack(padx=5, pady=5)

    # 输入框
    entry_role_type = tk.Entry(role_info_window)
    entry_role_type.pack(padx=5, pady=5)

    # 显示框
    display_text = tk.Text(role_info_window, height=10, width=40)
    display_text.pack()

    # 显示按钮
    def show_role_info():
        role_type = entry_role_type.get()
        role_data = get_role_info(role_type)
        display_text.delete(1.0, tk.END)
        display_text.insert(tk.END, role_data)

    show_button = tk.Button(role_info_window, text="显示角色信息", command=show_role_info)
    show_button.pack()

    # 提示文本
    label_prompt = tk.Label(role_info_window, text="其他功能：")
    label_prompt.pack(side=tk.LEFT, padx=5, pady=5)

    # 增加按钮
    def modify_role_info():
        role_info_window.destroy()  # Close the current window
        open_add_role_page()  # Open the add role information page

    modify_button = tk.Button(role_info_window, text="增加角色信息", command=modify_role_info)
    modify_button.pack(side=tk.LEFT, padx=5, pady=5)

    # 删除按钮
    def delete_role_info():
        role_info_window.destroy()  # Close the current window
        open_delete_role_page()  # Open the delete role information page

    delete_button = tk.Button(role_info_window, text="删除角色信息", command=delete_role_info)
    delete_button.pack(side=tk.LEFT, padx=5, pady=5)

    role_info_window.mainloop()


def get_role_info(role_type):
    try:
        # 连接到数据库
        conn = pymssql.connect(
            host='localhost',
            user='CSY',
            password='211002624',
            database='植物管理系统',
            as_dict=True
        )
        cursor = conn.cursor()

        # 执行查询语句
        query = f"SELECT 账号, 姓名, 密码, 职位 FROM 角色 WHERE 职位 = '{role_type}'"
        cursor.execute(query)

        # 获取查询结果
        role_data = cursor.fetchall()

        # 关闭数据库连接
        conn.close()

        # 格式化结果
        formatted_data = ""
        for data in role_data:
            formatted_data += f"账号: {data['账号']}, 姓名: {data['姓名']}, 密码: {data['密码']}, 职位: {data['职位']}\n"

        return formatted_data
    except Exception as e:
        print(f"Error: {e}")
        return "查询失败，请检查数据库连接和查询语句。"
